package com.sisyphus.spinach.helper;

import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;

/**
 * 富文本工具帮助类
 */
public class RichTextHelper {

    public static RichTextHelper newBuilder(CharSequence text) {
        return new RichTextHelper(text);
    }

    private int start, end;
    private SpannableStringBuilder style;

    private RichTextHelper(CharSequence text) {
        style = new SpannableStringBuilder(text);
        this.start = 0;
        this.end = text.length();
    }


    /**
     * 在以有文本上追加文本，并选中
     */
    public RichTextHelper append(CharSequence text) {
        return selected(style.length(), style.append(text).length());
    }


    /**
     * 设置选择范围，默认最后
     */
    public RichTextHelper selected(int start) {
        return selected(start, style.length());
    }


    /**
     * 设置选择范围
     */
    public RichTextHelper selected(int start, int end) {
        this.start = start;
        this.end = end;
        return this;
    }


    /**
     * 中划线
     */
    public RichTextHelper striketrough() {
        return striketrough(this.start, this.end);
    }


    /**
     * 背景色
     */
    public RichTextHelper backgroundColor(@ColorInt int color) {
        style.setSpan(new BackgroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }


    /**
     * 前景色
     */
    public RichTextHelper foregroundColor(@ColorInt int color) {
        style.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }


    /**
     * 点击事件
     */
    public RichTextHelper click(View.OnClickListener clickable) {
        return click(this.start, this.end, clickable);
    }


    /**
     * 字体大小百分比
     */
    public RichTextHelper size(float proportion) {
        return size(proportion, start, end);
    }


    /**
     * 字体加粗
     */
    public RichTextHelper bold() {
        return bold(start, end);
    }


    /**
     * 添加drawable
     */
    public RichTextHelper drawable(Drawable drawable) {
        return drawable(drawable, start, end);
    }


    public RichTextHelper click(int start, int end, View.OnClickListener clickable) {
        style.setSpan(new ClickableSpan() {

            @Override
            public void onClick(@NonNull View widget) {
                clickable.onClick(widget);
            }

        }, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        return this;
    }


    public RichTextHelper striketrough(int start, int end) {
        style.setSpan(new StrikethroughSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    public RichTextHelper size(float proportion, int start, int end) {
        style.setSpan(new RelativeSizeSpan(proportion), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    public RichTextHelper backgroundColor(@ColorInt int color, int start, int end) {
        style.setSpan(new BackgroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    public RichTextHelper foregroundColor(@ColorInt int color, int start, int end) {
        style.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    public RichTextHelper bold(int start, int end) {
        style.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    public RichTextHelper drawable(Drawable drawable, int start, int end) {
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        style.setSpan(new ImageSpan(drawable), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    public SpannableStringBuilder build() {
        return this.style;
    }

}
